<template>
  <div>
    <!-- 查询表单区域 -->
    <!-- 搜索框 -->
    <el-card class="box-card card-toubu" shadow="never">
      <div class="text item">
        <i class="el-icon-search"></i>&nbsp;
        <span>筛选搜索</span>
        <br />
        <br />
		<el-form :inline="true" :model="template" class="demo-form-inline">
		  <el-form-item label="品牌编号">
			<el-input v-model="template.id" placeholder="请输入模板编号"></el-input>
		  </el-form-item>
		  <el-form-item label="品牌名称">
			<el-input
			  v-model="template.name"
			  placeholder="请输入模板名称"
			></el-input>
		  </el-form-item>
		  <el-form-item>
			<el-button type="primary" @click="onSubmit">查询</el-button>
			<!-- 添加品牌按钮 -->
			<el-button
			  type="primary"
			  @click="
				dialogFormVisible = true;
				addOrUpdate = '添加';
			  "
			  >新增模板</el-button
			>
		  </el-form-item>
		</el-form>
	  </div>
	</el-card>
	<el-card class="box-card card-toubu" shadow="never">
	  <div class="text item">
	    <i class="el-icon-tickets"></i>&nbsp;
	    <span>数据列表</span>
	  </div>
	</el-card>
    <!-- 表格区域 -->
    <el-table :data="tableData" style="width: 100%">
      <el-table-column label="模板ID" width="280" prop="id"></el-table-column>
      <el-table-column
        label="模板名称"
        width="280"
        prop="name"
      ></el-table-column>
      <el-table-column
        label="规格数量"
        width="250"
        prop="specNum"
      ></el-table-column>
      <el-table-column
        label="参数数量"
        width="250"
        prop="paraNum"
      ></el-table-column>
      <el-table-column label="设置" width="280" prop="letter">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="spec(scope.row.id)"
            style="color: #2C3E50"
            >规格列表</el-button
          >
          <el-button
            size="mini"
            @click="para(scope.row.id)"
            style="color: #2C3E50"
            >参数列表</el-button
          >
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="
              handleEdit(scope.row.id);
              addOrUpdate = '编辑';
            "
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页控件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-sizes="pageSizes"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
    <!-- dialog对话框控件 -->
    <el-dialog
      :title="addOrUpdate + '模板'"
      :visible.sync="dialogFormVisible"
      @close="queryTemplate = {}"
    >
      <el-form :model="queryTemplate">
        <el-form-item label="模板名称" :label-width="formLabelWidth">
          <el-input v-model="queryTemplate.name" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handelAddOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 数据接收数组
      tableData: [],
      // 总行数
      total: 0,
      // 选择显示每页多少数据数组
      pageSizes: [10, 20, 30, 40],
      // 页号
      currentPage: 1,
      // 页大小
      pageSize: 10,
      // 查询表单数据
      template: {
        id: "",
        name: ""
      },
      // 对话框展示，默认不展示
      dialogFormVisible: false,
      // 判断对话框是添加还是修改
      addOrUpdate: "",
      // dialog对话框输入框长度
      formLabelWidth: "120px",
      // dialog对话框数据
      queryTemplate: {
        name: "",
        specNum: "",
        paraNum: ""
      }
    };
  },
  methods: {
    // 获取数据的方法
    async getTemplate() {
      const { data: res } = await this.$http.post(
        `vi/goods/template/pageQueryByAll/${this.currentPage}/${this.pageSize}`,
        this.template
      );
      this.tableData = res.data.pageUtils.rows;
      this.total = res.data.pageUtils.total;
    },
    // 通过Id查询品牌方法
    async handleEdit(id) {
      const { data: res } = await this.$http.get(
        `vi/goods/template/detail/${id}`
      );
      this.queryTemplate = res.data.tbTemplate;
      this.dialogFormVisible = true;
    },
    // 通过Id删除模板方法
    handleDelete(id) {
      this.$confirm("此操作将永久删除该记录, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          this.$http.delete(`vi/goods/template/delete/${id}`);
          this.$message.success("删除成功！");
          // 刷新页面
          this.getTemplate();
        })
        .catch(() => {
          this.$message.info("已取消删除");
        });
    },
    // 页大小改变方法
    handleSizeChange(val) {
      this.pageSize = val;
      this.getTemplate();
    },
    // 页号改变方法
    handleCurrentChange(val) {
      this.currentPage = val;
      this.getTemplate();
    },
    // 查询方法
    onSubmit() {
      this.getTemplate();
    },
    // 添加或修改模板方法
    async handelAddOrUpdate() {
	  if (this.queryTemplate.id) {
		  const { data: res } = await this.$http.put(`vi/goods/template/update`, this.queryTemplate)
		  this.$message.success(res.message);
	  } else {
		  const { data: res } = await this.$http.post(`vi/goods/template/add`, this.queryTemplate)
		  this.$message.success(res.message);
	  }
      this.dialogFormVisible = false;
      this.getTemplate();
    },
    // 路由跳转到Spec规格组件
    spec(id) {
      this.$router.push({ path: "/goods/spec", query: { templateId: id } });
    },
    // 路由跳转到Para参数组件
    para(id) {
      this.$router.push({ path: "/goods/para", query: { templateId: id } });
    }
  },
  created() {
    this.getTemplate();
  }
};
</script>

<style></style>
